package com.lanmaoly.cunzheng.callback;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {

	public static void requestData(final String urlStr, final HttpCallBackListener listener) {
		System.out.println("���뷽��-------------->");
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("new Thread-------------->");
				HttpURLConnection connection = null;
				try {
					URL url = new URL(urlStr);
					connection = (HttpURLConnection) url.openConnection();
					connection.setRequestMethod("POST");
					connection.setConnectTimeout(8000);
					connection.setReadTimeout(8000);
					connection.setDoInput(true);
					connection.setDoOutput(true);
					InputStream in = connection.getInputStream();
					BufferedReader br = new BufferedReader(new InputStreamReader(in));
					StringBuilder sb = new StringBuilder();
					String line;
					while ((line = br.readLine()) != null) {
						sb.append(line);
					}
					if (listener != null) {
						// �ص�onFinish����
						listener.onFinish(sb.toString());
					}
				} catch (Exception e) {
					if (listener != null) {
						// �ص�onError����
						listener.onError(e);
					}
				} finally {
					if (connection != null) {
						connection.disconnect();
					}
				}
			}
		}).start();
		System.out.println("��������-------------->");
	}
}
